import { randomUUID } from '../../../../mapUtils/RandomUtil';
import Event from './event';

/**
 * 专题图图层接口
 * @interface
 */
class IThematic {
  constructor(tool, options) {
    this.tool = tool;
    this.opts = options;
    this.id = randomUUID();
    this._refresh = this.refresh.bind(this);
    this.bindEvents = options.bindEvents || ['click'];
    this.events = {
      click: null,
      mousemove: null,
      mouseover: null,
      mouseout: null,
      datachange: null
    };
    this.eventTool = new Event(this);
    this.initLayers();
  }

  /**
   * 获取地图对象
   */
  getMap() {
    return this.tool.getMap();
  }

  /**
   * 初始化专题图层
   */
  initLayers() {
    this.opts.initLayers(this);
    this.getMap().on('zoomend', this._refresh);
    this.getMap().on('moveend', this._refresh);
    this.eventTool.addEvents();
  }

  /**
   * 移除图层
   */
  removeLayer() {
    this.eventTool.removeEvents();
    this.getMap().off('zoomend', this._refresh);
    this.getMap().off('moveend', this._refresh);
    this.layerIds.forEach((layerId) => {
      this.getMap().removeLayer(layerId);
    });
    this.getMap().removeSource(this.sourceId);
  }

  /**
   * 显示专题图层
   */
  show() {
    this.layerIds.forEach((layerId) => {
      if(this.getMap().getLayer(layerId)) {
        this.getMap().setLayoutProperty(layerId, 'visibility', 'visible');
      }
    });
    this.getMap().on('zoomend', this._refresh);
    this.getMap().on('moveend', this._refresh);
    this.refresh();
  }

  /**
   * 隐藏专题图层
   */
  hide() {
    this.layerIds.forEach((layerId) => {
      if(this.getMap().getLayer(layerId)) {
        this.getMap().setLayoutProperty(layerId, 'visibility', 'none');
      }
    });
    this.getMap().off('zoomend', this._refresh);
    this.getMap().off('moveend', this._refresh);
  }

  /**
   * 设置图层数据
   * @param {featureCollection} featureCollection 图层数据
   */
  setData(featureCollection) {
    this.getMap().getSource(this.sourceId).setData(featureCollection);
  }

  /**
   * 清除数据
   */
  clear() {
    this.getMap().getSource(this.sourceId).setData(turf.featureCollection([]));
  }

  /**
   * 刷新图层
   */
  refresh() {
    if(this.opts.refresh) {
      this.opts.refresh();
    }
  }

  /**
   * 根据图层Id修改图层显示状态
   * @param {Array} layerIds 修改图层Id
   * @param {String} visibility 是否显示
   */
  changeLayerStatusByIds(layerIds, visibility) {
    layerIds.forEach((layerId) => {
      if(this.getMap().getLayer(layerId)) {
        this.getMap().setLayoutProperty(layerId, 'visibility', visibility);
      }
    });
  }

  /**
   * 根据图层前缀修改图层显示状态
   * @param {Array} layerIdPrefixes 修改图层前缀
   * @param {String} visibility 是否显示
   */
  changeLayerStatusByPrefix(layerIdPrefixes, visibility) {
    let layers = [];
    let totalLayers = Object.keys(this.getMap().style._layers);
    totalLayers.forEach((layerId) => {
      if (this.hasSamePrefix(layerIdPrefixes, layerId)) {
        layers.push(layerId);
      }
    });
    layers.forEach((layerId) => {
      if(this.getMap().getLayer(layerId)) {
        this.getMap().setLayoutProperty(layerId, 'visibility', visibility);
      }
    });
  }

  /**
   * 图层Id是否含有指定前缀
   * @param {Array} layerIdPrefixes 图层前缀集合
   * @param {String} layerId 图层Id
   * @ignore
   */
  hasSamePrefix(layerIdPrefixes, layerId) {
    return layerIdPrefixes.some((item) => {
      return layerId.indexOf(item) === 0;
    });
  }
}

export default IThematic;
